﻿using Landpy.CruiseControlNet.ConfigurationSidekick.ParserModule;
using Landpy.CruiseControlNet.ConfigurationSidekick.Resources;
using Landpy.CruiseControlNet.FacadeInterface.ConfigurationModule;

namespace Landpy.CruiseControlNet.ConfigurationSidekick.ConfigTreeModule
{
    class NUnitHandler : UpdateChildrenNode
    {
        public override void UpdateChildrenNodes(ChildrenNodeContext childrenNodeContext)
        {
            var treeNodeIndentity = this.GetTreeNodeIndentity(childrenNodeContext);
            if (treeNodeIndentity.Type == TreeNodeType.NUnitTask &&
                childrenNodeContext.ChangedValueKey == typeof(Assembly).Name)
            {
                var assemblysTreeNode = childrenNodeContext.SelectedNode.Nodes[Resource.TreeNode_Assemblies];
                assemblysTreeNode.Remove();
                ProjectChildCollectionKey taskKey = ProjectChildCollectionKeyUtil.GetKey(treeNodeIndentity.Key);
                var queryProject = this.ProjectRepository.GetProjectById(childrenNodeContext.CruiseControl,
                                                                         taskKey.ProjectId);
                if (queryProject != null)
                {
                    var nunit = this.TaskRepository.GetTaskById(queryProject, taskKey.Id) as NUnit;
                    var treeNodesBuilder = this.GetTreeNodesBuilder(childrenNodeContext.CruiseControl);
                    var newAssemblysTreeNode = treeNodesBuilder.BuildAssemblysNode(nunit, childrenNodeContext.SelectedNode, taskKey.ProjectId, taskKey.Id);
                    childrenNodeContext.SelectedNode.Expand();
                    newAssemblysTreeNode.Expand();
                }
            }
            else
            {
                this.Successor.UpdateChildrenNodes(childrenNodeContext);
            }
        }
    }
}
